﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Devices.Sensors;
using Microsoft.Phone.Info;



namespace TCP_Socket
{
    public class Akcelerometr : PhoneApplicationPage
    {
        Accelerometer _myAccelerometer;
        MainPage _mainPage;
        SocketClient _client;
      
      

        public Akcelerometr(MainPage mainPage, SocketClient client)
        {
            _client = client;
            _myAccelerometer = new Accelerometer();
            this._mainPage = mainPage;

        
        }

        public void AkcStart()
        {
            try
            {
                _myAccelerometer.Start();
                _myAccelerometer.ReadingChanged += _myAccelerometer_ReadingChanged;
            }
            catch (AccelerometerFailedException ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        public void AkcStop()
        {
            try
            {
                _myAccelerometer.Stop();
                _myAccelerometer.ReadingChanged -= _myAccelerometer_ReadingChanged;
            }
            catch (AccelerometerFailedException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void _myAccelerometer_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
        {
            
            this.Dispatcher.BeginInvoke(delegate()
            {
                //mainPage.Log(e.X.ToString() + e.Y.ToString() + e.Z.ToString(), true);
                
                _mainPage.textBlock_X.Text = e.X.ToString("F4");

                _mainPage.textBlock_Y.Text = e.Y.ToString("F4");

                _mainPage.textBlock_Z.Text = e.Z.ToString("F4");

                string data = _mainPage.GetDeviceUniqueID() +" " + e.X.ToString("F4") + " " + e.Y.ToString("F4") + " " + e.Z.ToString("F4");

                _client.Send(data);

            });

        }

    }
}
